package edu.xhu;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import com.bean.ScoreBean;
import com.constant.Constant;
import com.data.GradeViewStateData;
import com.data.ScoreQueryData;
import com.net.ClientError;
import com.net.LoadData;
import com.net.RequestListener;
import com.net.RequestParameter;

import edu.xhu.adapters.EduSystemGradesAdapter;
import edu.xhu.view.LoadingDialog;

import android.R.integer;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.Toast;

public class EduSystemGrades extends Activity{

	private Button mBtnBack, mBtnQuery, mBtnPackUp;
	private XhuApplication application;
	private String viewStateString = null;
	private ArrayList<ScoreBean> mScoreList;
	private EduSystemGradesAdapter adapter;
	private ListView gradeListView;
	private Spinner spinnerYear, spinnerTerm, spinnerQueMethod;
	
	private RelativeLayout mChooseCondition, mChangeCondition;
	
	private ArrayList<String> yearStrings = new ArrayList<String>();
	private String[] termStrings = {"1", "2", "3"};
	private String[] queryStrings = {"按学期查询", 
									 "按学年查询",
									 "在校学习成绩查询"};
	
	private String year;
	private String term = "1";
	private String method = "按学期查询";
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.edusystem_grades);
		
		initView();
		setListener();
	}
	
	private void initView(){
		application				= (XhuApplication) getApplication();
		gradeListView			= (ListView) findViewById(R.id.listview_grades);
		mBtnBack				= (Button) findViewById(R.id.button_back);
		mBtnQuery				= (Button) findViewById(R.id.button_query);
		spinnerYear				= (Spinner) findViewById(R.id.year);
		spinnerTerm				= (Spinner) findViewById(R.id.term);
		spinnerQueMethod		= (Spinner) findViewById(R.id.method);
		mChooseCondition		= (RelativeLayout) findViewById(R.id.choose_condition);
		mChangeCondition		= (RelativeLayout) findViewById(R.id.change_condition);
		mBtnPackUp				= (Button) findViewById(R.id.button_packup);
		
		Date date 				= new Date();
		SimpleDateFormat format = new SimpleDateFormat("yyyy");
		String res				= format.format(date);
		int k = 2001;			//从2001年开始生成年分
		int lastYear = Integer.parseInt(res);
		while (k <= lastYear){
			String s = lastYear + "-" + (lastYear + 1);
			yearStrings.add(s);
			lastYear--;
		}
		
		ArrayAdapter spinnerAdapter = new ArrayAdapter<String>(EduSystemGrades.this,
				R.layout.spinner_style, yearStrings);
		spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		
		spinnerYear.setAdapter(spinnerAdapter);
		spinnerTerm.setAdapter(getAdapter(termStrings));
		spinnerQueMethod.setAdapter(getAdapter(queryStrings));
	}
	
	private void loadData(){
		String rMethodString = null;
		try {
			rMethodString = URLEncoder.encode(method, "utf-8");
			method = rMethodString;
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		RequestParameter parameter = new RequestParameter();
		parameter.add("url", application.getGradeUrl());
		parameter.add("__VIEWSTATE", viewStateString);
		parameter.add("ddlXN", year);
		parameter.add("ddlXQ", term);
		parameter.add("Button1", method);
		parameter.add("name", application.getUserName().replaceAll("同学", ""));
		
		LoadData.loadData(Constant.SCORE_QUERY_DATA, parameter, new RequestListener() {
			public void onError(ClientError ed) {
				LoadingDialog.dismiss();
				ErrorShow.showError(EduSystemGrades.this, ed);
			}
			
			public void onComplete(Object obj) {
				ScoreQueryData data = (ScoreQueryData) obj;
				
				mScoreList = data.getScoreList();
				if (null == mScoreList){
					LoadingDialog.dismiss();
					Toast.makeText(EduSystemGrades.this, "没有数据", Toast.LENGTH_SHORT).show();
					return;
				}
				
//				data.printData();
				
				mChooseCondition.setVisibility(View.GONE);
				mChangeCondition.setVisibility(View.VISIBLE);
				
				adapter = new EduSystemGradesAdapter(EduSystemGrades.this, mScoreList);
				gradeListView.setAdapter(adapter);
				
				LoadingDialog.dismiss();
			}
		});
	}
	
	private ArrayAdapter getAdapter(String[] strings){
		ArrayAdapter spinnerAdapter = new ArrayAdapter<String>(EduSystemGrades.this,
				R.layout.spinner_style, strings);
		spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		return spinnerAdapter;
	}
	
	private void getViewState(){
		mBtnBack.post(new Runnable() {
			public void run() {
				LoadingDialog.showWindow(EduSystemGrades.this);
			}
		});
		
		RequestParameter parameter = new RequestParameter();
		parameter.add("url", application.getGradeUrl());
		parameter.add("referer", application.getRefererUrl());
		parameter.add("name", application.getUserName().replaceAll("同学", ""));
		
		LoadData.loadData(Constant.GRADE_VIEW_STATE_DATA, parameter, new RequestListener() {
			public void onError(ClientError ed) {
				LoadingDialog.dismiss();
				ErrorShow.showError(EduSystemGrades.this, ed);
			}
			
			public void onComplete(Object obj) {
				GradeViewStateData data = (GradeViewStateData) obj;
				viewStateString = data.getViewState();
				
				loadData();
			}
		});
	}
	
	
	private void setListener(){
		mBtnBack.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				finish();
			}
		});
		
		mBtnQuery.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				getViewState();
			}
		});
		
		spinnerYear.setOnItemSelectedListener(new OnItemSelectedListener() {
			public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				year = yearStrings.get(arg2);
			}
			public void onNothingSelected(AdapterView<?> arg0) {
			}
		});
		
		spinnerTerm.setOnItemSelectedListener(new OnItemSelectedListener() {
			public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				term = termStrings[arg2];
			}
			public void onNothingSelected(AdapterView<?> arg0) {
			}
		});
		
		spinnerQueMethod.setOnItemSelectedListener(new OnItemSelectedListener() {
			public void onItemSelected(AdapterView<?> arg0, View arg1,int arg2, long arg3) {
				method = queryStrings[arg2];
			}
			public void onNothingSelected(AdapterView<?> arg0) {
			}
		});
		
		mChangeCondition.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				mChooseCondition.setVisibility(View.VISIBLE);
				mChangeCondition.setVisibility(View.GONE);
			}
		});
		
		mBtnPackUp.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				mChooseCondition.setVisibility(View.GONE);
				mChangeCondition.setVisibility(View.VISIBLE);
			}
		});
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK){
			if (LoadingDialog.isShowing())
				LoadingDialog.dismiss();
			else {
				finish();
			}
			return true;
		}
		
		return super.onKeyDown(keyCode, event);
	}
}
